package com.bdqn.controller;

import com.bdqn.pojo.Right;
import com.bdqn.pojo.Role;
import com.bdqn.pojo.User;
import com.bdqn.service.RoleService;
import com.bdqn.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class IndexController {
    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;
    /*@Resource
    private StringReaderTemplate stringReaderTemplate;*/
    private String SHIRO_LOGIN_COUNT="shiro_login_count";

    @RequestMapping(value = "/403")
    public String unauthorized(){
        return "403";
    }

    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @RequestMapping("/main")
    public String toMain(){
        return "main";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("loginUser");
        session.invalidate();
        SecurityUtils.getSubject().logout();
        return "redirect:/login";
    }

    @PostMapping("/login")
    public String doLogin(String usrName, String usrPassword, HttpServletRequest request,Model model,HttpSession session){
      /* User user=userService.login(usrName, usrPassword);
       if (user!=null){
          request.getSession().setAttribute("loginUser", user);
          return "redirect:/main";
       }else{
          request.setAttribute("message","登录失败，用户名或密码错误！");
           return "/login";
       }*/
        UsernamePasswordToken token = new UsernamePasswordToken(usrName, usrPassword);
        Subject subject = SecurityUtils.getSubject();
        subject.login(token);
        User user = (User) subject.getPrincipal();

        try {
            token = new UsernamePasswordToken(usrName, usrPassword);
            subject = SecurityUtils.getSubject();
            subject.login(token);
            user = (User) subject.getPrincipal();
            Role role=user.getRole();
            List<Right> rights=roleService.findRightsByRole(role);
            role.getRights().addAll(rights);
            session.setAttribute("loginUser",user);
            return "redirect:/main";
        }catch (UnknownAccountException | IncorrectCredentialsException e){
            model.addAttribute("message","用户名或密码失败，登录失败！");
            return "login";
        }catch (LockedAccountException e){
            model.addAttribute("message","用户被禁用，登录失败！");
            return "login";
        }catch (AuthenticationException e){
            model.addAttribute("message","认证异常，登录失败！");
            return "login";
        }
    }
   /* @RequestMapping(value = "/login")
    public String login(String userName,String password,HttpSession session,Model model){
        try{
             userName=userService.encryptPassword(password);
            UsernamePasswordToken token=new UsernamePasswordToken(userName,userName);
            Subject subject= SecurityUtils.getSubject();
            subject.login(token);
            return "redirect:/main";
        }catch (Exception e){
            model.addAttribute("message",e.getMessage());
            return "login";
        }
    }*/
}
